package com.lxj.word.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.utils.common.CommonResult;
import com.lxj.utils.entity.Word;
import com.lxj.word.mapper.WordMapper;
import com.lxj.word.service.WordService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class WordServiceImpl implements WordService {
    @Autowired
    private WordMapper wordMapper;

    /**
     * 查询所有的单词
     *
     * @param word
     * @return
     */
    @Override
    public CommonResult findAll(Word word) {
        PageHelper.startPage(word.getPageNum(), word.getPageSize());
        PageInfo<Word> info = new PageInfo<>(this.wordMapper.findAll(word));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", info.getList());
        map.put("number", info.getTotal());
        return CommonResult.success(map);
    }

    /**
     * 进行点赞或者取消点赞，这里应该添加事物，
     * 根据用户名先去判断是否已经对改单词点赞，
     * 如果没有 则进行数据增加，热度+1（操作单词表），如果有则删除关联表数据，热度-1.
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult like(Integer id, String username) {
        //判断是否点赞
        int aa = judge(id, username);
        if (aa==1){
            //取消点赞
            int bb = wordMapper.delete(id,username);
            int cc = wordMapper.subtract(id);
            System.out.println(bb);
            System.out.println(cc);
            return CommonResult.success(100);
        }else {
            //增加点赞
            int result = wordMapper.add(id,username);
            int result1 = wordMapper.addHeat(id);
            return CommonResult.success(200);
        }
    }

    private Integer judge(Integer id, String username) {
       return wordMapper.judge(id,username);
    }

    /**
     * 对单词进行收藏，与点赞逻辑一模一样，也需要判断是否收藏过，并对热度进行操作
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult collect(Integer id, String username) {
        int aa =wordMapper.judgeCollect(id,username);
        if (aa==1){
            //取消收藏
            int bb = wordMapper.cancel(id,username);
            System.out.println(bb);
            return CommonResult.success(1);
        }else {
            //收藏
            int result = wordMapper.collect(id,username);
            System.out.println(result);
            return CommonResult.success(2);
        }
    }

    @Override
    public CommonResult findLike(String username) {
       List<Integer> list = wordMapper.findLike(username);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult findCollect(String username) {
        List<Integer> list = wordMapper.findCollect(username);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult myCollect(Word word) {
        List<Integer> list = wordMapper.findCollect(word.getUsername());
        word.setList(list);
        PageHelper.startPage(word.getPageNum(),word.getPageSize());
        PageInfo<Word> info = new PageInfo<>(this.wordMapper.myCollect(word));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult cancelCollect(Integer id, String username) {
        int result = wordMapper.cancelCollect(id,username);
        if (result==1){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult mySubmit(Word word) {
        PageHelper.startPage(word.getPageNum(),word.getPageSize());
        PageInfo<Word> info = new PageInfo<>(this.wordMapper.mySubmit(word));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult add(Word word) {
        int count = wordMapper.newAdd(word);
        if (count==1){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult findById(Integer id) {
        Word word = wordMapper.findById(id);
        return CommonResult.success(word);
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int result = wordMapper.deleteSub(id);
        if (result==1){
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult edit(Word word) {
        int edit = wordMapper.edit(word);
        if (edit==1){
            return CommonResult.success(edit);
        }
        return CommonResult.failed();
    }
}
